package co.za.alicrity.exactcover;

import co.za.alicrity.exactcover.linkedlist.Iterator;
import co.za.alicrity.exactcover.linkedlist.LinkedList;
import co.za.alicrity.exactcover.linkedlist.LinkedListHelper;
import co.za.alicrity.exactcover.linkedlist.ListNode;

public class ColumnHeader implements ListNode<ColumnHeader>,LinkedList<Node>{
	
	private ColumnHeader nextColumn;
	private ColumnHeader prevColumn;
	
	private Node firstNode;
	private Node lastNode;
	
	private LinkedListHelper<ColumnHeader,Node> helper;
	
	public ColumnHeader(){
		helper = new LinkedListHelper<ColumnHeader,Node>(this);
	}
	
	public Node getNodeAtRow(int row){
		Iterator<Node> i = helper.getIterator();
		while(i.hasNext()){
			Node ret=i.getNext();
			if (ret.getRow()==row){
				return ret;
			}
		}
		return null;
	}
	
	public boolean nodeExistsForRow(int row){
		Iterator<Node> i = helper.getIterator();
		while(i.hasNext()){
			if (i.getNext().getRow()==row){
				return true;
			}
		}
		return false;
	}
	
	public void addNode(Node node){
		helper.addNode(node);
	}
	@Override
	public Node getFirst() {
		return firstNode;
	}
	@Override
	public Node getLast() {
		return lastNode;
	}
	@Override
	public ColumnHeader getNext() {
		return nextColumn;
	}
	@Override
	public ColumnHeader getPrevious() {
		return prevColumn;
	}
	@Override
	public void setFirst(Node node) {
		firstNode=node;
	}
	@Override
	public void setLast(Node node) {
		lastNode=node;
	}
	@Override
	public void setNext(ColumnHeader node) {
		nextColumn=node;
	}
	@Override
	public void setPrevious(ColumnHeader node) {
		prevColumn= node;
	}
	
	public String toString(){
		return helper.getListString();
	}

	public int getSize() {
		return helper.getSize();
	}

	public Iterator<Node> getIterator() {
		return helper.getIterator();
	}
	
	public Node getNodeAt(int z){
		int c=0;
		Iterator<Node> i = helper.getIterator();
		while(i.hasNext()){
			if (z==c){
				return i.getNext();
			}
			c++;
		}
		return null;
	}

	public Node remove(int i) {
		Node ret=getNodeAtRow(i);
		if (ret!=null)
			helper.removeNode(ret);
		return ret;
	}

	public Node remove(Node node) {
		helper.removeNode(node);
		return node;
	}

	public void putBack(Node node) {
		helper.putBack(node);
	}

}
